﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using POS.Framework;
using DevExpress.Xpo;
using BLL;
using Model.Bill;
using DevExpress.Data.Filtering;
using BLL.Enums;
using POS.ModForm.Sale;

namespace POS.UserControl.Dialog
{
    public partial class frmPopDelivery : DevExpress.XtraEditors.XtraForm
    {
        public frmPopDelivery()
        {
            InitializeComponent();
        }

        private void frmPopDelivery_Load(object sender, EventArgs e)
        {
            this.gridView.CustomDrawRowIndicator += new DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventHandler(SpecialGridView.DrawRowIndicator);
            SetButtonClickEvent();
            SearchData();
        }
        void SetButtonClickEvent()
        {
            this.popFormButtonUC.OK.Click += new EventHandler(btnOK_Click);
            this.gridView.DoubleClick += new EventHandler(btnOK_Click);
        }

        void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                TB_Order order = this.gridView.GetFocusedRow() as TB_Order;
                if (order == null)
                {
                    MsgBox.ShowWarning("请选择要结算的送货订单！");
                    return;
                }
                using (frmPayMent fpm = new frmPayMent(order))
                {
                    if (fpm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        MsgBox.ShowTips("送货结算售成功！");
                        this.SearchData();
                    }
                }
            }
            catch (Exception ex)
            {
                MsgBox.ShowWarning(ex.Message);
                return;
            }
        }
        void SearchData()
        {
            GroupOperator goFilter = new GroupOperator();
            goFilter.Operands.Add(CriteriaOperator.Parse("CreateDT>=?", DateTime.Now.ToString("yyyy-MM-dd")));
            goFilter.Operands.Add(CriteriaOperator.Parse("TypeID=? and PayStatusID<>?", (int)OrderType.Delivery, (int)PaymentStatus.Complete));
            XPCollection<TB_Order> orders = new XPCollection<TB_Order>(DatabaseSchema.Session, goFilter);
            this.grid.DataSource = orders;
            SpecialGridView.SetGridViewIndicatorWidth(orders.Count, this.gridView);
        }
    }
}